package com.haiyi.activiti.engine.framework.activiti.manager.impl;

import com.haiyi.activiti.engine.model.entity.RuleEngine;
import com.haiyi.activiti.engine.exception.RuleException;
import com.haiyi.activiti.engine.framework.activiti.manager.RuleEngineManager;
import com.haiyi.activiti.engine.framework.basic.DateTimeAdapter;
import com.haiyi.activiti.engine.framework.basic.IdBuilder;
import com.haiyi.activiti.engine.framework.basic.SecurityAdapter;
import com.haiyi.activiti.engine.framework.ruleengine.RuleEngineEnum;
import com.haiyi.activiti.engine.framework.ruleengine.RuleEngineFactory;
import com.haiyi.activiti.engine.framework.ruleengine.Stream;
import com.haiyi.activiti.engine.query.RuleEngineQuery;
import com.haiyi.activiti.engine.service.IRuleEngineService;
import com.haiyi.activiti.engine.util.Assert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/6 15:02 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class RuleEngineManagerImpl implements RuleEngineManager {

    private static final Logger log = LoggerFactory.getLogger(RuleEngineManagerImpl.class);

    @Autowired
    private IRuleEngineService ruleEngineService;
    @Autowired
    private IdBuilder idBuilder;
    @Autowired
    private SecurityAdapter securityAdapter;
    @Autowired
    private DateTimeAdapter dateTimeAdapter;
    @Autowired
    private RuleEngineFactory factory;

    @Override
    public RuleEngineQuery createRuleEngineQuery() {
        return ruleEngineService.createQuery();
    }

    @Override
    public void createRuleEngine(RuleEngine ruleEngine) {
        if(ruleEngine.getIid()==null){
            ruleEngine.setIid(idBuilder.getId());
        }
        ruleEngine.setCreater(securityAdapter.getCurrentUser().getIid());
        ruleEngine.setCreateTime(dateTimeAdapter.getCurrentDate());
        Assert.isTrue(ruleEngineService.insert(ruleEngine),"规则新增失败！");
    }

    @Override
    public void updateRuleEngine(RuleEngine ruleEngine) {
        Assert.isNotNull(ruleEngine,"规则id不能为空！");
        Assert.isTrue(ruleEngineService.updateById(ruleEngine),"规则修改失败！");
    }

    @Override
    public void deleteRuleEngine(String iid) {
        Assert.isNotNull(iid,"规则id不能为空！");
        Assert.isTrue(ruleEngineService.deleteById(iid),"规则删除失败！");
    }

    @Override
    public Stream executeRule(RuleEngine ruleEngine, Map<String,Object> params) {
        final String expression = ruleEngine.getExpression();
        if(StringUtils.isNoneBlank(expression)) {
            log.info("要执行的规则引擎表达式：{}" + expression);
            RuleEngineEnum.getByCode(ruleEngine.getLanguage());
            return factory.getRequestServiceByType(ruleEngine.getLanguage())
                    .execute(ruleEngine, params);
        }else{
            throw new RuleException("规则引擎配置项为空，规则引擎执行失败！");
        }
    }
}
